package com.cll.pmis.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cll.pmis.common.Result;
import com.cll.pmis.common.annotation.HasPerms;
import com.cll.pmis.common.security.SecurityContext;
import com.cll.pmis.common.validator.ValidatorUtils;
import com.cll.pmis.common.validator.group.Insert;
import com.cll.pmis.entity.Dossier;
import com.cll.pmis.entity.Orders;
import com.cll.pmis.entity.Prescribe;
import com.cll.pmis.model.PrescribeVo;
import com.cll.pmis.service.IDossierService;
import com.cll.pmis.service.IPrescribeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  医生开药
 * </p>
 *
 * @author chenliangliang
 * @since 2018-06-18
 */
@RestController
@RequestMapping("/prescribe")
public class PrescribeController {


    private IPrescribeService prescribeService;
    private IDossierService dossierService;

    protected PrescribeController(IPrescribeService prescribeService,IDossierService dossierService){
        this.prescribeService=prescribeService;
        this.dossierService=dossierService;
    }

    @HasPerms("prescribe:insert")
    @PostMapping
    public Result prescribe(@RequestBody PrescribeVo prescribes){
        ValidatorUtils.validateEntity(prescribes,Insert.class);

        for (Prescribe prescribe:prescribes.getList()){
            ValidatorUtils.validateEntity(prescribe,Insert.class);
        }

        Dossier dossier = dossierService.selectOne(new EntityWrapper<Dossier>().eq("id", prescribes.getDossierId()));
        if (dossier==null){
            return Result.fail("病例不存在");
        }
        //创建订单
        Orders order = prescribeService.createPrescribe(dossier, SecurityContext.getUsername(), prescribes.getList());

        return Result.ok(order);
    }
}

